﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Data;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using SilverlightCommands;
using System.ServiceModel;
using KMT_Pro.Web;
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Client;
using System.Collections.ObjectModel;
using System.Windows.Media.Imaging;
using System.IO;

namespace KMT_Pro.ViewModels.HardwareViewModels
{
    public class HardwareDetailInfoViewModel : MessageSendableViewModel
    {
        #region Constants and Fields

        CustomerContext _context;

        public string hardwareID = string.Empty;
        public string hardwareName = string.Empty;
        public string hardwareType = string.Empty;
        public string companyName = string.Empty;
        public string companyTelNum = string.Empty;
        public string aSlimit = string.Empty;
        public string etc = string.Empty;
        public bool isReadOnly = true;
        public string gubun;

        BitmapImage imageSource = new BitmapImage();

        #endregion

        #region Constructors and Destructors

        public HardwareDetailInfoViewModel()
        {
            this.editClick = new RelayCommand((p) =>
                                                {
                                                    if (this.IsReadOnly)
                                                        gubun = "U"; //수정
                                                    else
                                                        gubun = "I"; //신규

                                                    try
                                                    {
                                                        this.IsBusy = true;

                                                        _context = new CustomerContext();

                                                        var q = _context.Load(_context.HardwareMaster_InsertQuery(this.HardwareID
                                                                                                                 , this.HardwareName
                                                                                                                 , this.HardwareType
                                                                                                                 , this.CompanyName
                                                                                                                 , this.CompanyTelNum
                                                                                                                 , this.ASlimit
                                                                                                                 , DateTime.Now.ToString()
                                                                                                                 , this.Etc
                                                                                                                 , this.gubun));
                                                        q.Completed += EditComplete; //비동기 완료 이벤트 등록
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        this.IsBusy = false;
                                                        ChildWindow errorWin = new ErrorWindow(ex);
                                                        errorWin.Show();
                                                    }
                                                });
            this.addImgClick1 = new RelayCommand((p) =>
            {
                
            });
        }

        #endregion

        #region Properties

        // ------------------------ 속성 : Start -----------------------------------

        public bool IsReadOnly
        {
            set
            {
                this.isReadOnly = value;
                this.OnPropertyChanged("IsReadOnly");
            }
            get
            {
                return isReadOnly;
            }
        }

        public string HardwareID
        {
            set
            {
                this.hardwareID = value;
                this.OnPropertyChanged("HardwareID");
            }
            get
            {
                return hardwareID;
            }
        }

        public string HardwareName
        {
            set
            {
                this.hardwareName = value;
                this.OnPropertyChanged("HardwareName");
            }
            get
            {
                return hardwareName;
            }
        }

        public string HardwareType
        {
            set
            {
                this.hardwareType = value;
                this.OnPropertyChanged("HardwareType");
            }
            get
            {
                return hardwareType;
            }
        }

        public string CompanyName
        {
            set
            {
                this.companyName = value;
                this.OnPropertyChanged("CompanyName");
            }
            get
            {
                return companyName;
            }
        }

        public string CompanyTelNum
        {
            set
            {
                this.companyTelNum = value;
                this.OnPropertyChanged("CompanyTelNum");
            }
            get
            {
                return companyTelNum;
            }
        }

        public string ASlimit
        {
            set
            {
                this.aSlimit = value;
                this.OnPropertyChanged("ASlimit");
            }
            get
            {
                return aSlimit;
            }
        }
        public string Etc
        {
            set
            {
                this.etc = value;
                this.OnPropertyChanged("Etc");
            }
            get
            {
                return etc;
            }
        }

        // ------------------------ 속성 : End -------------------------------------

        //------------------------- 버튼 : Start -----------------------------------

        public ICommand editClick { get; set; }

        public ICommand addImgClick1 { get; set; }

        public ICommand addImgClick2 { get; set; }
        //------------------------- 버튼 : End -------------------------------------

        #endregion

        #region Event

        /// <summary>
        /// 수정 완료시 비동기 처리
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void EditComplete(object sender, EventArgs e)
        {
            LoadOperation lo = sender as LoadOperation;
            try
            {
                foreach (ResultMessage GAP in lo.AllEntities)
                {
                    MessageBox.Show(GAP.ExceptionMessage);
                    this.IsBusy = false;
                }
            }
            catch (Exception)
            { this.IsBusy = false; }
        }

        public void ImgAddComplete(object sender, EventArgs e)
        {
            LoadOperation lo = sender as LoadOperation;
            try
            {
                foreach (ResultMessage GAP in lo.AllEntities)
                {
                    MessageBox.Show(GAP.ExceptionMessage);
                    this.IsBusy = false;
                }
            }
            catch (Exception)
            { this.IsBusy = false; }
        }

        #endregion

        #region Method

        public void addImg(string HardwareID, Byte[] Image1 )
        {
            try
            {
                _context = new CustomerContext();

                var q = _context.Load(_context.HardwareImage_InsertQuery(HardwareID, Image1, "Insert"));
                q.Completed += ImgAddComplete; //비동기 완료 이벤트 등록
            }
            catch (Exception)
            { }
        }

        #endregion

    }
}
